package com.coincalf.exchange.job;

import com.coincalf.exchange.service.MarketService;
import com.coincalf.exchange.task.MatchTask;
import com.coincalf.framework.constants.Constant;
import com.coincalf.framework.enums.MarketType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Description: 撮合任务
 * @Author: Chen Long
 * @Date: Created in 2018/5/13 下午1:54
 * @Modified by: Chen Long
 */
@Component
@Slf4j
public class CoincalfExchangeJob implements Constant {

    @Autowired
    private MarketService marketService;
    @Autowired
    private MatchTask matchTask;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 币币交易撮合
     */
    @Scheduled(fixedRate = 10)
    public void tradeMatch() {
        marketService.queryMarketListFromCache(MarketType.TRADE).forEach(market -> {
            // 币币交易撮合队列Redis存储Key
            String redisKey = new StringBuffer(REDIS_KEY_TRADE_MATCH).append(market.getSymbol()).toString();
            // 委托订单号
            String orderId = redisTemplate.opsForList().leftPop(redisKey);
            if (!StringUtils.isEmpty(orderId)) {
                matchTask.tradeMatch(market, orderId);
            }
        });
    }

}
